﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using Microsoft.Practices.Web.UI.WebControls;
using Ornament.MemberShip;
using Ornament.MemberShip.Module.Presenters;
using Ornament.MemberShip.Module.Views;

public partial class Admin_MemberAdmin_Controls_OrgEditorView : UserControl, IOrgEditorView
{
    /// <summary>
    ///获取一个值指示当前被选择的节点是否为根节点
    /// </summary>
    protected bool SelectedRoot
    {
        get { return treeView1.SelectedNode == null || treeView1.SelectedNode.Value == "root"; }
    }

    #region IOrgEditorView Members

    public OrgEditorPresenter Presenter { get; set; }

    public void BindToTree(IEnumerable<Org> result)
    {
        treeView1.Nodes[0].ChildNodes.Clear();
        BindOrgTreeNode(result, treeView1.Nodes[0]);
    }

    public void BindEditingOrg(Org org)
    {
        orgFormView.ChangeMode(FormViewMode.Edit);
        orgForViewDataSource.DataSource = new List<Org> {org};
        ;
        orgForViewDataSource.DataBind();
    }

    public IRoleAssignView RoleAssignView
    {
        get { return RoleAssign1; }
    }

    public IOrgAssignView OrgAssignView
    {
        get { return orgAssign1; }
    }

    public Guid ParentOfEditingOrg
    {
        get
        {
            var SelectedParentOrgCode =
                (HtmlInputHidden) orgFormView.Row.Controls[0].FindControl("parentOrgCodeHiddenBox");
            Guid parentId = Guid.Empty;
            if (SelectedParentOrgCode.Value.Length != 0 && SelectedParentOrgCode.Value != "root")
                parentId = new Guid(SelectedParentOrgCode.Value);
            return parentId;
        }
    }

    #endregion

    private static void BindOrgTreeNode(IEnumerable<Org> orgs, TreeNode parent)
    {
        foreach (Org org in orgs)
        {
            var node = new TreeNode {Text = org.Name, Value = org.Id.ToString()};
            parent.ChildNodes.Add(node);
            ReadOnlyCollection<Org> childNodes = org.GetAllChilds();
            if (childNodes.Count != 0)
            {
                BindOrgTreeNode(childNodes, node);
            }
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        Presenter = new OrgEditorPresenter(this);
        if (!IsPostBack)
        {
            Presenter.InitilizeView();
            orgAssign1.ClientClickFunctionMethod = "SelectedParent";
        }
        Presenter.ViewLoad();
    }


    protected void addNewOrg(object sender, EventArgs e)
    {
        if (orgFormView.CurrentMode != FormViewMode.Insert)
        {
            orgFormView.ChangeMode(FormViewMode.Insert);
        }
    }


    protected void dataSourceCreateObject(object sender, ObjectContainerDataSourceCreatingObjectArgs e)
    {
        switch (e.CurrentOperation)
        {
            case ObjectContainerOperator.Insert:
                e.NewObjectInstance = new Org();
                break;
            default:
                e.NewObjectInstance = Presenter.GetOrg((Guid) e.Keys["Id"]);
                break;
        }
    }


    protected void dataSourceGetCurrentOrg(object sender, ObjectContainerDataSourceSelectingEventArgs e)
    {
        if (treeView1.SelectedNode != null && !SelectedRoot)
        {
            Presenter.SetEditingOrg(new Guid(treeView1.SelectedNode.Value));
        }
    }


    protected void OnMenuItemClick(object sender, EventArgs e)
    {
        if (treeView1.SelectedNode != null && !SelectedRoot)
        {
            orgFormView.DataBind();
            orgFormView.ChangeMode(FormViewMode.Edit);
        }
    }

    protected void removeNewOrg(object sender, EventArgs e)
    {
        throw new NotImplementedException();
    }

    protected void OrgInserted(object sender, ObjectContainerDataSourceStatusEventArgs e)
    {
        Presenter.SaveChange((Org) e.Instance);
    }

    protected void OrgUpdated(object sender, ObjectContainerDataSourceStatusEventArgs e)
    {
        Presenter.SaveChange((Org) e.Instance);
    }
}